package cn.nfwx.tv.controller;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.adapter.SearchAdapter;
import cn.nfwx.tv.model.TVProgramModel;
import cn.nfwx.tv.util.RecyclerViewUtil;
import cn.nfwx.tv.widget.LoadView;

/**
 * Created by dell on 2017/11/15.
 */

public class SearchResultController {

    private String keyword;
    private RecyclerView recyclerView;
    private SearchAdapter mAdapter;

    private LoadView loadView;


    public SearchResultController(View view) {
        loadView = new LoadView(view.getContext());
        loadView.setBackgroundColor(Color.WHITE);
        recyclerView = findView(view, R.id.search_lookback_recyclerView);
        initRecyclerView(recyclerView);
    }

    public SearchResultController(Activity activity) {
        loadView = new LoadView(activity);
        loadView.setBackgroundColor(Color.WHITE);
        recyclerView = findView(activity, R.id.search_lookback_recyclerView);
        initRecyclerView(recyclerView);
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public void initRecyclerView(RecyclerView recyclerView) {
        RecyclerViewUtil.setLinearLayoutManagerWithoutDivder(recyclerView.getContext(), recyclerView);
        mAdapter = new SearchAdapter();
        mAdapter.setEmptyView(loadView);
//        mAdapter.setEmptyView(loadView);
//        showLoading();
        recyclerView.setAdapter(mAdapter);
    }


    public SearchAdapter getAdapter() {
        return mAdapter;
    }


    /**
     * 查找控件
     *
     * @param rid
     * @param <T>
     * @return
     */
    public <T extends View> T findView(View view, int rid) {
        return (T) view.findViewById(rid);
    }

    public <T extends View> T findView(Activity activity, int rid) {
        return (T) activity.findViewById(rid);
    }

    public void setNewData(List<Object> newData) {
        mAdapter.setNewData(newData);
        setNodata();
    }

    public void setNodata() {
        loadView.stopLoading();
        loadView.setVisibility(View.VISIBLE);
        String nodata = loadView.getContext().getString(R.string.search_nodata, keyword);
        String nodateNote = loadView.getContext().getString(R.string.search_nodata_note);
        loadView.loadNodata(nodata, nodateNote);
    }


    /**
     * 预约
     *
     * @param position
     */
    public void appointmentNotifyData(int position) {
        mAdapter.notifyItemChanged(position);
    }

    /**
     * 订阅
     *
     * @param position
     */
    public void subscriptionNotifyData(int position) {
        mAdapter.notifyItemChanged(position);
    }

    public void addData(ArrayList objects) {
        mAdapter.addData(objects);
    }

    public void showLoading() {
        loadView.startLoading();
        loadView.setVisibility(View.VISIBLE);
        mAdapter.setNewData(new ArrayList<>());
    }

    public void showError() {
        loadView.loadError();
        loadView.setVisibility(View.VISIBLE);
        mAdapter.setNewData(new ArrayList<>());
    }

    public void hiddenLoadView() {
        loadView.setVisibility(View.INVISIBLE);
    }

    public void onLoadClickListener(LoadView.OnLoadClickListener click) {
        loadView.setOnLoadClickLisener(click);
    }

    public void bindColumnData(List<Object> subscriptionModels) {
        mAdapter.addData(0, subscriptionModels);
        recyclerView.smoothScrollToPosition(0);
        hiddenLoadView();
        if (!subscriptionModels.isEmpty()) {
            hiddenLoadView();
        }
    }

    public void bindVodData(List<Object> columns) {
        mAdapter.addData(columns);
        hiddenLoadView();
        if (!columns.isEmpty()) {
            hiddenLoadView();
        }
    }

    public void bindijkPrograms(List programs) {
        mAdapter.addData(programs);
        hiddenLoadView();
    }

    public void clearData() {
        mAdapter.setNewData(new ArrayList<>());
    }

    public void bindProgramsData(ArrayList<TVProgramModel> _programs_trailer) {
        ArrayList<Object> objects = new ArrayList<>();
        objects.addAll(_programs_trailer);
        mAdapter.setNewData(objects);
        recyclerView.smoothScrollToPosition(0);
        if (!_programs_trailer.isEmpty()) {
            hiddenLoadView();
        }
    }

    public void release() {
        loadView.release();
    }
}
